package lumis.portal.presentation.tag.page;

import lumis.portal.PortalException;
import lumis.portal.PortalStringResource;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageConfig;
import lumis.portal.presentation.LoadingMessage;
import lumis.portal.presentation.core.HTMLFragmentComponent;
import lumis.portal.presentation.core.LayoutFilePage;
import lumis.portal.presentation.perspective.Perspective;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.IResource;
import lumis.util.PortalUtil;
import lumis.util.TextUtil;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.util.string.AppendingStringBuffer;

/**
 * Injects the page's head scripts components.
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class PageHeadScriptsComponent extends HTMLFragmentComponent 
{
	/**
	 * Serial version identifier.
	 * @since 6.0.0
	 */
	private static final long serialVersionUID = 1L;

	public PageHeadScriptsComponent(String id, boolean autoRenderBodyOnly) 
	{
		super(id, autoRenderBodyOnly);
	}

	@Override
	protected CharSequence getHTMLFragment(MarkupStream markupStream, ComponentTag openTag) 
	{
		try
		{
			AppendingStringBuffer headScripts = new AppendingStringBuffer(7000);
			
			SessionConfig sessionConfig = SessionConfig.getCurrentSessionConfig();
			LayoutFilePage page = ((LayoutFilePage)getPage());
			PageConfig pageConfig = page.getPageWebResource().getPage();
			
			IResource resource = new PortalStringResource();
			String localizedPageName = ManagerFactory.getLocalizationManager().localize(pageConfig.getName(), resource);
			headScripts.append(PortalUtil.generateClientScript("<script type=\"text/javascript\">//<![CDATA[\n var g_LumisLocale = \"" + sessionConfig.getLocale() + 
					"\";var g_LumisDisplayPerspective = " + page.getPortalMode().getPerspective().value() + "; var g_LumisChannelId = \"" + pageConfig.getChannelId() +
					"\"; var g_LumisRootChannelId = \"" + page.getRootChannelId() + "\"; var g_LumisPageId = \"" + pageConfig.getId() + 
					"\"; var g_LumisPageConfig = new Object();g_LumisPageConfig.name = \"" + localizedPageName.replaceAll("\"", "\\\\\"") + 
					"\"; g_LumisPageConfig.type = " + pageConfig.getType() + "; g_LumisPageConfig.isTemplate = " + pageConfig.isTemplate() + 
					"; g_LumisPageConfig.hasParentTemplate = " + (pageConfig.getParentTemplateId() != null) + "; var g_LumisDisplayMode = " + page.getPortalMode().getId() +
					";var g_LumisLayoutFile = " + (page.getLayoutFilePath() != null) + ";") + "var g_LumisUserPerspective = " + Perspective.USER.value() +
					";var g_LumisAdminPerspective = " + Perspective.ADMIN.value() + ";" + "var g_LumisPublisherPerspective = " + Perspective.PUBLISHER.value() + ";");
					

			ChannelConfig channelConfig = ManagerFactory.getChannelManager().get(sessionConfig, pageConfig.getChannelId(), PortalTransactionFactory.getCurrentTransaction());
			String localizedChannelName = ManagerFactory.getLocalizationManager().localize(channelConfig.getName(), resource);
			headScripts.append(PortalUtil.generateClientScript("var g_LumisChannelConfig = new Object(); g_LumisChannelConfig.name = \"" + localizedChannelName.replaceAll("\"", "\\\\\"") + "\";"));
			headScripts.append(PortalUtil.generateClientScript("var g_LumisIsHtmlCache=false;"));
			headScripts.append(PortalUtil.generateClientScript("var g_LumisRootPath='" + ((WebRequest)getRequest()).getHttpServletRequest().getContextPath() +"/';"));
			headScripts.append(PortalUtil.generateClientScript("var g_LumisLoadingHtmlMessage=\""+TextUtil.escapeJs(LoadingMessage.getLoadingMessage())+"\";"));
			
			if (page.isForSelenium())
			{
				headScripts.append(PortalUtil.generateClientScript("var $_LumAlert = function(msg){\n"));
				headScripts.append(PortalUtil.generateClientScript("	window.setTimeout(function() { window.alert(msg) }, 10);\n"));
				headScripts.append(PortalUtil.generateClientScript("}; window.alert = $_LumAlert;"));
			}

			headScripts.append(PortalUtil.generateClientScript("\n//]]>\n</script>"));
			headScripts.append(PortalUtil.generateClientScript("<script type=\"text/javascript\" charset=\"UTF-8\" src=\"lumis/portal/client/script/LumisPortal.js\"></script>"));
			
			if(page.getHighlightInterfaceInst() != null)
			{
				// include javascript to scroll the interface instance into view
				headScripts.append(PortalUtil.generateClientScript("<script type=\"text/javascript\">LumisPortal.bindReady(function(){document.getElementById(\"H_"+page.getHighlightInterfaceInst()+"\").scrollIntoView(false);});</script>"));
			}
			
			return headScripts;
		}
		catch (PortalException e)
		{
			throw new RuntimeException(e);
		}
	}
}
